﻿using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;
using System;
using ASPNETMVCApplication.Code.Filters;
using ASPNETMVCApplication.Repositories;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Code.HtmlHelpers;
using ASPNETMVCApplication.Controllers;
using ASPNETWebApplication;
using ASPNETMVCApplication.Areas.Supp.Models;
using ASPNETMVCApplication.Areas.Shop.Models;

namespace ASPNETMVCApplication.Areas.Cust.Controllers
{
    public class OrderInfoController : Controller
    {

        private ICategoryRepository _categoryRepository;
        private IProductRepository _productRepository;
        private IOrderRepository _orderRepository;
        private ISupplierRepository _supplierRepository;
        public OrderInfoController()
            : this(new CategoryRepository(), new ProductRepository(), new OrderRepository(),new SupplierRepository())
        {
        }

        public OrderInfoController(ICategoryRepository categoryRepository, IProductRepository productRepository, IOrderRepository orderRepository,ISupplierRepository supplierRepository)
        {
            _categoryRepository = categoryRepository;
            _productRepository = productRepository;
            _orderRepository = orderRepository;
            _supplierRepository = supplierRepository;
        }
        //
        // GET: /Cust/OrderInfo/

        public ActionResult Index()
        {
            try
            {
                List<Supplier> listSupplier = _supplierRepository.GetList(null).ToList();
                List<SelectListItem> listItem = new List<SelectListItem>();
                foreach (Supplier supp in listSupplier)
                {
                    listItem.Add(new SelectListItem { Text = supp.CompanyName, Selected = false, Value = supp.SupplierId.ToString() });
                }
                List<SelectListItem> status = new List<SelectListItem>();
                status.Add(new SelectListItem { Text="Đã thanh toán",Value="1",Selected=true});
                status.Add(new SelectListItem { Text = "Chưa thanh toán", Value = "0", Selected = false });
                ViewData["Status"] = status;
                ViewData["ListSupp"] = listItem;
                return View();            
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ActionResult CustGetOrderList()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CustGetOrderList(OrderModel orderModel)
        {
            if (ModelState.IsValid)
            {
                orderModel.CustomerId = (int)Session["custId"];
                if (orderModel.DateFrom == null) orderModel.DateFrom = DateTime.Now;
                if (orderModel.DateThru == null) orderModel.DateThru = DateTime.Now;
                List<Order> orderlist = _orderRepository.GetOrderList(orderModel.CustomerId, orderModel.SupplierId, orderModel.DateFrom, orderModel.DateThru, orderModel.Status);

                return View(orderlist);
            }
            else
            {
                return Index();
            }
        }

        public ActionResult Details(int id)
        {
            var order = _orderRepository.Get(id);
            List<OrderDetail> listOrderDetails = order.OrderDetails.ToList<OrderDetail>();
            return View(listOrderDetails);
        }
    }
}
